<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\AdminBaseController;
use app\portal\model\PortalPostModel;
use app\portal\service\PostService;
use app\portal\model\PortalCategoryModel;
use think\Db;
use think\Image;

//调查问卷中心
class AdminQuestController extends AdminBaseController
{

    //列表
    public function index(){
        $log_list = Db::name('quest')
        ->where(['delete_time'=>0])
        ->order("start_time desc")
        ->paginate(11);
        
        // 获取分页显示
        $page = $log_list->render();
        
        $this->assign("page", $page);
        $data = $log_list->all();
        
        $answer = Db::name('quest_answer')
        ->select();
        $title = Db::name('quest_title')
        ->select();
        
        for ($i=0,$count=count($data); $i < $count; $i++) { 
            $data[$i]['answer1'] = 0;
            $data[$i]['answer2'] = 0;
            $data[$i]['answer3'] = 0;
            $data[$i]['num'] = 0;
            $data[$i]['counttitle'] = 0;
            
            foreach ($answer as $key => $value) {
            
                if ($data[$i]['id'] == $value['quest_id']) {
                    $data[$i]['num'] += 1;
                    $answer_array = json_decode($value['answer'],true);
                    $count_array = array_count_values($answer_array);
                    $data[$i]['answer1'] += $count_array[1];
                    $data[$i]['answer2'] += $count_array[2];
                    $data[$i]['answer3'] += $count_array[3];
                }
            }
            foreach ($title as $key2 => $value2) {
                if ($data[$i]['id'] == $value2['qid']) {
                    $data[$i]['counttitle'] += 1;
                }
            }
            $data[$i]['sum'] = $data[$i]['counttitle']*$data[$i]['num'];
        }
        $this->assign("data", $data);
        return $this->fetch();
    }
    
    
    //删除提交
    public function delete(){
        $param   = $this->request->param();
        if (isset($param['id'])) {
            $id   = $this->request->param('id', 0, 'intval');
            $data = Db::name('quest')->find($id);
            $res = Db::name('quest')->where(['id'=>$id])->update(['delete_time'=>time()]);
            //Db::name('quest_title')->where(['qid'=>$id])->delete();
            if ($res) {
                $data   = [
                    'object_id'   => $data['id'],
                    'create_time' => time(),
                    'table_name'  => 'quest',
                    'name'        => $data['name'],
                    'site_id'     => 1,  //站点ID
                ];
                Db::name('recycleBin')->insert($data);
                //操作日志
                admin_log($data['name'], 'delete', '评价中心调查问卷');

                $this->success('删除成功!', url('AdminQuest/index'));
            }else{
                $this->error('删除失败');
            }
        }
    }
    //编辑
    public function edit()
    {
        $id   = $this->request->param('id',0,'intval');
        $info = Db::name('quest')->find($id);
        $this->assign('info',$info);
        return $this->fetch();
    }
    //新增问卷
    public function add()
    {
        return $this->fetch();
    }  
    //新增提交
    public function addPost()
    {
        $data   = $this->request->param();
        $param = $data['post'];
        if (empty($param['name'])) {
            $this->error('标题不能为空');
        }
        if (empty($param['intro'])) {
            $this->error('简介不能为空');
        }
        $param['end_time'] = strtotime($param['end_time']);

        if ($param['end_time']  < time()) {
            $this->error('结束时间必须晚于当前实际时间');
        }
        $param['start_time'] = time();
        $res = Db::name('quest')->insert($param);
        if ($res) {
            //操作日志
            admin_log($param['name'], 'add', '评价中心调查问卷');
            $this->success('成功!', url('AdminQuest/index'));
        }else{
            $this->error('失败');
        }
    }
    //编辑提交
    public function editPost()
    {
        $data   = $this->request->param();
        $param = $data['post'];
        $id   = $this->request->param('id', 0, 'intval');
        if (empty($param['name'])) {
            $this->error('标题不能为空');
        }
        if (empty($param['intro'])) {
            $this->error('简介不能为空');
        }
        $param['end_time'] = strtotime($param['end_time']);

        if ($param['end_time']  < time()) {
            $this->error('结束时间必须晚于当前实际时间');
        }
        $res = Db::name('quest')->where(['id'=>$id])->update($param);
        if ($res) {
            //操作日志
            admin_log($param['name'], 'edit', '评价中心调查问卷');
            $this->success('成功!', url('AdminQuest/index'));
        }else{
            $this->error('失败');
        }
    }

    //问题管理
    public function title()
    {
        $id = $this->request->param('id',0,'intval');
        $log_list = Db::name('quest_title')
            ->where(['qid'=>$id,'delete_time'=>0])
            ->order("id")
            ->paginate(11);
        
        // 获取分页显示
        $page = $log_list->render();
       
        $this->assign("page", $page);
  
        $this->assign("data", $log_list);
        return $this->fetch();
    }
    //新增问题
    public function addTitle()
    {
        return $this->fetch();
    }
    //编辑问题
    public function editTitle()
    {
        $id   = $this->request->param('id',0,'intval');
        $info = Db::name('quest_title')->find($id);
        $this->assign('info',$info);
        return $this->fetch();
    }
    //新增问题
    public function addTitlePost()
    {
        $data   = $this->request->param();
        $param = $data['post'];
        if (empty($param['title'])) {
            $this->error('题目不能为空');
        }
        $res = Db::name('quest_title')->insert($param);
        if ($res) {
            //操作日志
            admin_log($param['title'], 'add', '评价中心调查问卷的问题');
            $this->success('成功!', url('AdminQuest/title',array('id'=>$param['qid'])));
        }else{
            $this->error('失败');
        }
    }
    //编辑问题提交
    public function editTitlePost()
    {
        $data   = $this->request->param();
        $param = $data['post'];
        $id   = $this->request->param('id', 0, 'intval');

        $param = $data['post'];
        if (empty($param['title'])) {
            $this->error('题目不能为空');
        }
        $info = Db::name('quest_title')->where(['id'=>$id])->find();
        $res = Db::name('quest_title')->where(['id'=>$id])->update($param);
        
        if ($res) {
            //操作日志
            admin_log($param['title'], 'edit', '评价中心调查问卷的问题');
            $this->success('成功!', url('AdminQuest/title',array('id'=>$info['qid'])));
        }else{
            $this->error('失败');
        }
    }
    //删除问题
    public function deleteTitle()
    {
        $param   = $this->request->param();
        if (isset($param['id'])) {
            $id   = $this->request->param('id', 0, 'intval');
            $qid   = $this->request->param('qid', 0, 'intval');
            $data = Db::name('quest_title')->find($id);
            $res = Db::name('quest_title')->where(['id'=>$id])->update(['delete_time'=>time()]);
            if ($res) {
                $data   = [
                    'object_id'   => $data['id'],
                    'create_time' => time(),
                    'table_name'  => 'quest_title',
                    'name'        => $data['title'],
                    'site_id'     => 1,  //站点ID
                ];
                Db::name('recycleBin')->insert($data);
                //操作日志
                admin_log($data['title'], 'delete', '评价中心调查问卷的问题');

                $this->success('删除成功!', url('AdminQuest/title',array('id'=>$qid)));
            }else{
                $this->error('删除失败');
            }
        }
    }
    //评价答案管理
    public function answerlist()
    {
        $id   = $this->request->param('id',0,'intval');
        $log_list = Db::name('quest_answer')
            ->where(['quest_id'=> $id])
            ->order("id")
            ->paginate(11);
        
        // 获取分页显示
        $page = $log_list->render();
        
        $this->assign("page", $page);

        $this->assign("data", $log_list);
        return $this->fetch();
    }
}
